package com.ksd.modules.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.message.entity.AdminMsgSystem;
import com.ksd.modules.message.mapper.AdminMsgSystemMapper;
import com.ksd.modules.message.po.AdminMsgSystemSavePO;
import com.ksd.modules.message.po.AdminMsgSystemSearchPO;
import com.ksd.modules.message.service.IAdminMsgSystemService;
import com.ksd.modules.message.vo.AdminMsgSystemVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminMsgSystemServiceImpl extends BaseServiceImpl<AdminMsgSystemMapper, AdminMsgSystem> implements IAdminMsgSystemService {


    /**
     * 查询列表
     * @param adminNoteSearchPO
     * @return
     */
    public Page<AdminMsgSystemVo> findAdminMsgSystems(AdminMsgSystemSearchPO adminNoteSearchPO){
        Page<AdminMsgSystem> page = new Page<>(adminNoteSearchPO.getPageNo(),adminNoteSearchPO.getPageSize());
        LambdaQueryWrapper<AdminMsgSystem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(AdminMsgSystem.class, c -> !c.getColumn().equalsIgnoreCase("content"));
        lambdaQueryWrapper.eq(AdminMsgSystem::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminMsgSystem::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminNoteSearchPO.getStype()), AdminMsgSystem::getStype,adminNoteSearchPO.getStype());
        lambdaQueryWrapper.eq(Objects.nonNull(adminNoteSearchPO.getStatus()), AdminMsgSystem::getStatus,adminNoteSearchPO.getStatus());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminNoteSearchPO.getKeyword())){
            lambdaQueryWrapper.like( AdminMsgSystem::getTitle,adminNoteSearchPO.getKeyword());
        }

        lambdaQueryWrapper.orderByDesc(AdminMsgSystem::getCreateTime);
        Page<AdminMsgSystem> adminNotePage = this.page(page, lambdaQueryWrapper);
        Page<AdminMsgSystemVo> adminNoteVoPage = tranferPageBo(adminNotePage, AdminMsgSystemVo.class);
        return adminNoteVoPage;
    }

    /**
     * 保存和更新
     * @param adminNoteSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminMsgSystemVo saveUpdateAdminMsgSystem(AdminMsgSystemSavePO adminNoteSavePO){
        AdminMsgSystem adminNote = tranferBo(adminNoteSavePO, AdminMsgSystem.class);
        adminNote.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminNote.setSystemName(SystemIdThrealLocal.get().getSystemName());
        boolean b = this.saveOrUpdate(adminNote);
        AdminMsgSystemVo adminNoteVo = tranferBo(adminNote, AdminMsgSystemVo.class);
        return adminNoteVo;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminMsgSystemVo getAdminMsgSystemById(Long id){
        AdminMsgSystem adminNote = this.getById(id);
        AdminMsgSystemVo adminNoteVo = tranferBo(adminNote, AdminMsgSystemVo.class);
        return adminNoteVo;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminMsgSystemById(Long id){
        boolean b = this.removeById(id);
        return b;
    }

    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminMsgSystem(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchDowns(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminMsgSystem> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.UN_STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchUps(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminMsgSystem> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }

    /**
     * 复制
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyData(Long opid){
        AdminMsgSystem data = this.getById(opid);
        if(Objects.nonNull(data)){
            data.setId(null);
            data.setStatus(0);
            data.setCreateTime(LocalDateTime.now());
            data.setUpdateTime(LocalDateTime.now());
            data.setTitle(data.getTitle()+"[副本]");
            this.save(data);
        }
        return true;
    }
}
